package giaodien;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.ImageIcon;
import javax.swing.JButton;

import model.KhachHang;
import controller.KhachHangController;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Color;
import javax.swing.UIManager;
import java.awt.SystemColor;
import javax.swing.border.TitledBorder;

public class ThemKhachHang extends JFrame {

	private JPanel contentPane;
	private JTextField textField;
	private JTextField textField_1;
	private JTextField textField_2;
	private JTextField textField_3;
	private JTextField textField_4;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ThemKhachHang frame = new ThemKhachHang();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ThemKhachHang() {
		setTitle("Thêm khách hàng");
		// setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 438, 361);
		contentPane = new JPanel();
		contentPane.setBorder(new TitledBorder(null, "", TitledBorder.LEADING,
				TitledBorder.TOP, null, null));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel lblNewLabel = new JLabel("Mã khách hàng");
		lblNewLabel.setBounds(23, 66, 98, 14);
		contentPane.add(lblNewLabel);

		JLabel lblThem = new JLabel("NHẬP THÔNG TIN KHÁCH HÀNG");
		lblThem.setForeground(SystemColor.textHighlight);
		lblThem.setBounds(23, 21, 240, 24);
		contentPane.add(lblThem);

		textField = new JTextField();
		textField.setBounds(159, 63, 114, 20);
		contentPane.add(textField);
		textField.setColumns(10);

		JLabel lblTen = new JLabel("Tên khách hàng");
		lblTen.setBounds(23, 104, 98, 16);
		contentPane.add(lblTen);

		textField_1 = new JTextField();
		textField_1.setBounds(159, 102, 114, 20);
		contentPane.add(textField_1);
		textField_1.setColumns(10);

		JLabel lblDiaChi = new JLabel("Địa chỉ");
		lblDiaChi.setBounds(23, 145, 98, 16);
		contentPane.add(lblDiaChi);

		textField_2 = new JTextField();
		textField_2.setBounds(159, 143, 114, 20);
		contentPane.add(textField_2);
		textField_2.setColumns(10);

		JLabel lblSdt = new JLabel("Số điện thoại");
		lblSdt.setBounds(23, 186, 98, 16);
		contentPane.add(lblSdt);

		textField_3 = new JTextField();
		textField_3.setBounds(159, 184, 114, 20);
		contentPane.add(textField_3);
		textField_3.setColumns(10);

		JLabel lblDiem = new JLabel("Điểm tích lũy");
		lblDiem.setBounds(23, 227, 98, 16);
		contentPane.add(lblDiem);

		textField_4 = new JTextField();
		textField_4.setBounds(159, 225, 114, 20);
		contentPane.add(textField_4);
		textField_4.setColumns(10);
		textField_4.setText("0");

		JLabel label = new JLabel("");
		label.setIcon(new ImageIcon(ThemKhachHang.class.getResource("/icon/add-user-icon.png")));
		label.setBounds(320, 45, 87, 93);
		contentPane.add(label);

		final JLabel label_1 = new JLabel("");
		label_1.setForeground(Color.RED);
		label_1.setBounds(299, 145, 114, 16);
		contentPane.add(label_1);

		final JLabel label_2 = new JLabel("");
		label_2.setForeground(Color.RED);
		label_2.setBounds(299, 173, 108, 16);
		contentPane.add(label_2);

		JButton btnThm = new JButton("Thêm");
		btnThm.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					int ma = Integer.parseInt(textField.getText());
					String tenKH = textField_1.getText();
					String diaChi = textField_2.getText();
					String sdt = textField_3.getText();
					double diemTichLuy = 0;
					if (Integer.toString(ma).equals("") || tenKH.equals("")
							|| diaChi.equals("") || sdt.equals("")
							|| Double.toString(diemTichLuy).equals("")) {
						label_1.setText("Vui lòng nhập");
						label_2.setText(" đầy đủ các trường");
					} else {
						KhachHangController.insertKhachHang(new KhachHang(ma,
								tenKH, diaChi, sdt, diemTichLuy));
						JOptionPane.showMessageDialog(null,
								"Thêm khách hàng thành công");
					}
				} catch (Exception e2) {
					label_1.setText("Nhập liệu bị lỗi");
					label_2.setText("vui lòng nhập lại!");
					textField.setText("");
					textField_1.setText("");
					textField_2.setText("");
					textField_3.setText("");
				}
			}
		});
		btnThm.setBounds(111, 264, 92, 26);
		contentPane.add(btnThm);

		JButton btnHy = new JButton("Hủy");
		btnHy.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnHy.setBounds(222, 264, 77, 26);
		contentPane.add(btnHy);

	}
}
